package com.maila.xiaoaoschool.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;
import com.google.gson.Gson;
import com.maila.xiaoaoschool.R;
import com.maila.xiaoaoschool.adapter.MainLocationAdapter;
import com.maila.xiaoaoschool.api.IndexApi;
import com.maila.xiaoaoschool.entity.IndexRecommendShop;
import com.maila.xiaoaoschool.net.VolleyInterface;
import com.zhy.autolayout.AutoRelativeLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * Created by mark on 2018/1/27.
 */

public class SearchShopActivity extends AppCompatActivity {

    @BindView(R.id.white_return_iv)
    AutoRelativeLayout whiteReturnIv;
    @BindView(R.id.tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.lv_fujin)
    ListView lvFujin;
    @BindView(R.id.hot_refresh)
    MaterialRefreshLayout hotRefresh;
    MainLocationAdapter mainLocationAdapter;
    Gson gson=new Gson();
    int Opt;
    private String lat = "36.71287";
    private String lng = "119.126626";
    private String adcode = "1";
    private int page = 1;
    private int size = 6;
    private String field = "evaluate";
    private String keyword = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchshop);
        ButterKnife.bind(this);
        mainLocationAdapter = new MainLocationAdapter(this);
        lvFujin.setAdapter(mainLocationAdapter);
        Opt = getIntent().getIntExtra("OPT", -1);
        switch (Opt) {
            case 1:
                tvShopName.setText("搜索结果");
                keyword = getIntent().getStringExtra("str");
                break;
            case 2:
                tvShopName.setText("超市列表");
                break;
            case 3:
                tvShopName.setText("自营店铺");
                break;
        }
        hotRefresh.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                hotRefresh.finishRefresh();
            }

            @Override
            public void onRefreshLoadMore(MaterialRefreshLayout materialRefreshLayout) {
                super.onRefreshLoadMore(materialRefreshLayout);
                initData();
            }
        });
        initData();
    }

    private void initData() {
        switch (Opt) {
            case 1:
                getSearchlist();
                break;
            case 2:
                getMarketlist();
                break;

            case 3:
                getziyinglist();
                break;
        }

    }

    /**
     * 获取自营列表
     */
    private void getziyinglist() {
        new IndexApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                IndexRecommendShop recommendShop = gson.fromJson(result, IndexRecommendShop.class);
                if (recommendShop.getResult() == 1) {
                    if (page == 1) {
                        mainLocationAdapter.setMdata(recommendShop.getData());
                    } else {
                        mainLocationAdapter.addMdata(recommendShop.getData());
                    }
                    hotRefresh.finishRefreshLoadMore();
                    ++page;

                } else {
                    hotRefresh.finishRefreshLoadMore();
                    hotRefresh.setLoadMore(false);
                    Toast.makeText(SearchShopActivity.this, "暂无更多店铺", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).get_ziying_list(adcode, page, size, lat, lng);
    }

    /**
     * 获取超市列表
     */
    private void getMarketlist() {

        new IndexApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                IndexRecommendShop recommendShop = gson.fromJson(result, IndexRecommendShop.class);
                if (recommendShop.getResult() == 1) {
                    if (page == 1) {
                        mainLocationAdapter.setMdata(recommendShop.getData());
                    } else {
                        mainLocationAdapter.addMdata(recommendShop.getData());
                    }
                    hotRefresh.finishRefreshLoadMore();
                    ++page;

                } else {
                    hotRefresh.finishRefreshLoadMore();
                    hotRefresh.setLoadMore(false);
                    Toast.makeText(SearchShopActivity.this, "暂无更多店铺", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).get_market_list(adcode, page, size, lat, lng);
    }

    /**
     * 获取搜索列表
     */
    private void getSearchlist() {
        new IndexApi(this, new VolleyInterface() {
            @Override
            public void onSuccess(String result) {
                IndexRecommendShop recommendShop = gson.fromJson(result, IndexRecommendShop.class);
                if (recommendShop.getResult() == 1) {
                    if (page == 1) {
                        mainLocationAdapter.setMdata(recommendShop.getData());
                    } else {
                        mainLocationAdapter.addMdata(recommendShop.getData());
                    }
                    hotRefresh.finishRefreshLoadMore();
                    ++page;

                } else {
                    hotRefresh.finishRefreshLoadMore();
                    hotRefresh.setLoadMore(false);
                    Toast.makeText(SearchShopActivity.this, "暂无更多店铺", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(VolleyError error) {

            }
        }).get_search_list(adcode, page, size, lat, lng, keyword);
    }

    @OnClick(R.id.white_return_iv)
    public void onViewClicked() {
        finish();
    }
}
